using Rewired;
using Steamworks;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace __Naptime.Script.Steam
{
	public class SteamOverlayHandler : MonoBehaviour
	{
		private Callback<GameOverlayActivated_t> m_GameOverlayActivated;

		private SettingsPanel _settingsPanel;

		private void Start()
		{
			m_GameOverlayActivated = Callback<GameOverlayActivated_t>.Create(OnGameOverlayActivated);
			ReInput.ControllerPreDisconnectEvent += OnControllerPreDisconnect;
		}

		private void OnDestroy()
		{
			m_GameOverlayActivated?.Dispose();
			ReInput.ControllerPreDisconnectEvent -= OnControllerPreDisconnect;
		}

		private void OnControllerPreDisconnect(ControllerStatusChangedEventArgs controllerStatus)
		{
			if (!(SceneManager.GetActiveScene().name != "GamePlay"))
			{
				if ((object)_settingsPanel == null)
				{
					_settingsPanel = Object.FindObjectOfType<SettingsPanel>();
				}
				_settingsPanel?.Setup();
			}
		}

		private void OnGameOverlayActivated(GameOverlayActivated_t pCallback)
		{
			if (pCallback.m_bActive != 0 && !(SceneManager.GetActiveScene().name != "GamePlay"))
			{
				if ((object)_settingsPanel == null)
				{
					_settingsPanel = Object.FindObjectOfType<SettingsPanel>();
				}
				_settingsPanel?.Setup();
			}
		}
	}
}
